import 'package:dio/dio.dart';
import 'package:yysc_flutter/config/Config.dart';

class DioManager {
  late Dio _dio;

  DioManager._internal() {
    BaseOptions options = BaseOptions(
      baseUrl: Config.baseUrl, // 你的 API 根 URL
      connectTimeout: const Duration(seconds: 5),
      receiveTimeout: const Duration(seconds: 300),
    );

    _dio = Dio(options);

    /// 添加拦截器（可选）
    _dio.interceptors.add(InterceptorsWrapper(
      onRequest: (options, handler) {
        // 添加公共请求头
        options.headers['Authorization'] = 'token';
        options.headers['ip'] = 'ip';
        options.headers['deviceId'] = 'deviceId';
        options.headers['versionName'] = 'versionName';
        return handler.next(options);
      },
      onResponse: (response, handler) {
        /// 处理响应数据
        return handler.next(response);
      },
      onError: (DioError e, handler) {
        /// 处理错误
        return handler.next(e);
      },
    ));
  }

  static final DioManager _instance = DioManager._internal();

  factory DioManager() {
    return _instance;
  }

  Dio get dio => _dio;
}
